#include "apue.2e/include/apue.h"
#include <sys/wait.h>

int main(void)
{
	char buf[MAXLINE];
	pid_t pid;
	int status;

	printf("%% ");

	while(fgets(buf,MAXLINE,stdin)!= NULL){
		printf("buf=%s",buf);

		if(buf[strlen(buf)-1] =='\n')
			buf[strlen(buf)-1] =0;

		if((pid=fork())<0){
			err_sys("fork error");
		}else if (pid ==0){
			printf("child process");
			execlp(buf,buf,(char*)0);
			err_ret("couldn't execute:%s",buf);
			exit(127);
		}

		/*parnet*/
		if((pid=waitpid(pid,&status,0))<0){
			err_sys("waitpid error");
		}
		printf("%% ");
	}
	exit(0);
}

/**
一个简单的shell
fork 子进程返回0,父进程中，返回子进程的pid


char *fgets(char *str, int n, FILE *stream)
reads a line from the specified stream and stores it into the string pointed to by str.
It stops when
either (n-1) characters are read,
the newline character is read,
or the end-of-file is reached,
whichever comes first.


execl, execlp, execle, execv, execvp, execvpe - execute a file
Synopsis
#include <unistd.h>
extern char **environ;

int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg, ..., char * const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[],char *const envp[]);
execlp()会
从PATH 环境变量所指的目录中查找符合参数file的文件名，找到后便执行该文件，
然后将第二个以后的参数当做该文件的argv[0]、argv[1]……，
最后一个参数必须用空指针(NULL)作结束。
*/
